<?php
namespace Nawa;
/**
 * Nawa Medical Center.
 *
 * This object represent a Medical Center.
 * It can either be an Hospital or a Fundation
 *
 * @author Xavier VILAIN
 * @package Nawa
 *
 */
use Fox\Common\Accounting\Currency\Label;

use Fox\Dao\Link;
use Fox\Dao;

class MedicalCenter extends \Fox\Object\Versioned
{
	/**
	 * Link to Address
	 */
	const LINK_ADDRESS = "address";

	/**
	 * Link to Medicals Studies
	 */
	const LINK_MEDICAL_STUDIES = "medicalStudies";

	/**
	 * Link to Medicals Studies Medical Center Partners
	 */
	const LINK_MEDICAL_STUDIES_CENTER_PARTNERS = "medicalStudiesCenterPartners";
	
	/**
	 * Link to Type
	 */
	const LINK_TYPE = "type";

	/**
	 * Link To Real Document Object
	 */
	const LINK_DOCUMENT = "document";

	/**
	 * (non-PHPdoc)
	 * @see \Fox\Object::getData()
	 */
	public function getData()
	{
		$data = parent::getNonProtectedData();
		return array_merge($data,$this->address()->getData());
	}

	/**
	 * (non-PHPdoc)
	 * @see Fox.Object::afterSetFields()
	 */
	protected function afterSetFields(array $fields)
	{
		if(isset($fields['name1']))
		$this->keyname = \Fox\Utilities::cleanup($fields['name1']);
		foreach($this->getProtectedFields() as $fieldName => $value)
		{
			unset($fields[$fieldName]);
		}
		unset($fields['adrid']);
		$this->address()->setFields($fields);
		return $fields;
	}

	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/\Fox\Object\Versioned::beforeVersionUpdate()
	 */
	protected function beforeVersionCreate() {
		// Build Address Link
		//$this->address()->create();
		//$this->adrid = $this->address()->getId();
	}

	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/\Fox\Object\Versioned::afterVersionCreate()
	 */
	protected function afterVersionCreate(\Fox\Version $version)
	{
		// Add Resume to the search engine
		$searchEngine = new \Fox\SearchEngine();
		$searchEngine->add($this,$this->getId(),self::className());
	}

	/**
	 * (non-PHPdoc)
	 * @see includes/classes/Fox/Object/\Fox\Object\Versioned::afterVersionUpdate()
	 */
	protected function afterVersionUpdate()
	{
		// Search Engine
		$searchEngine = new \Fox\SearchEngine();
		$searchEngine->synchronize($this,$this->getId(),self::classname());
	}

	/**
	 * Use for the Search Engine
	 * @return string All object attributes has a string
	 */
	public function content()
	{
		$data = $this->getData();
		$data['typelabel'] = $this->type()->label();
		return implode(",",$data);
	}

	/**
	 *
	 * Short to Define a Label
	 * @return string
	 */
	public function label()
	{
		return $this->name1;
	}
}
\Fox\Dao::infos(MedicalCenter::className(),new \Fox\Object\Info('medicalcenters', \Fox\Databases::DB_APP)
)->unique('keyname');

\Fox\Object\Links::add(\Fox\Dao::link(MedicalCenter::className(),MedicalCenter::LINK_ADDRESS,Link::FETCH_EAGER)
	->to(Address::className(), Address::LINK_MEDICAL_CENTER)
	->on('adrid')
	->oneToOne()
	->versionTo()
);
// Load Medical Study Links
MedicalStudy\MedicalCenter::className();

// Load Medical Study - Medical Center Partner Link
 \Fox\Object\Links::add(\Fox\Dao::link(MedicalStudy\MedicalCenter\Partner::className(), MedicalStudy\MedicalCenter\Partner::LINK_MEDICAL_CENTER)
 ->to(MedicalCenter::className(),MedicalCenter::LINK_MEDICAL_STUDIES_CENTER_PARTNERS)
 ->on('mcid')
 ->oneToOne()
 );

// Link to Type
\Fox\Object\Links::add(\Fox\Dao::link(MedicalCenter::className(),MedicalCenter::LINK_TYPE)
->to(MedicalCenter\Type::className(), MedicalCenter\Type::LINK_MEDICAL_CENTER)
->oneToOne()
->on('type')
->where('language',array(\Xview::instance()->language())));
// Link to Medical Study Medical Center Partners
